//TODO 优化编码
import { SET_WEBSITE_INFO, SET_FRIEND_LIST_INFO, WEBSITE_SYS_PARAMS } from '@/store/mutation-types'
import { getAllSysParams } from '@/api/blog'
import _ from 'lodash/core'
import Vue from 'vue'

const BlogIndex = {

  state: {
    friendListInfo: [],//友联
    websiteInfo: []//技术网站
  },

  actions: {
    /**
     * 初始化系统参数
     * @param commit
     * @param data
     */
    initSysParam({ commit }, { data }) {
      commit(SET_FRIEND_LIST_INFO, _.filter(data, { name: 'friendListInfo' }))
      commit(SET_WEBSITE_INFO, _.filter(data, { name: 'websiteInfo' }))
    },
    /**
     * 加载所有的系统配置参数
     * @param commit
     */
    loadAllSysParam({ commit }, {}) {
      getAllSysParams().then(function(res) {
        if (res.code === '200') {
          let friendListInfo = _.filter(res.data, { name: 'friendListInfo' })
          let websiteInfo = _.filter(res.data, { name: 'websiteInfo' })
          // 客户端缓存一个小时
          Vue.ls.set(WEBSITE_SYS_PARAMS, res.data, 60 * 60 * 1000)
          if (friendListInfo) {
            commit(SET_FRIEND_LIST_INFO, friendListInfo)
          }
          if (websiteInfo) {
            commit(SET_WEBSITE_INFO, websiteInfo)
          }
        }
      })
    }
  },

  mutations: {
    [SET_FRIEND_LIST_INFO]: (state, friendListInfo) => {
      state.friendListInfo = friendListInfo
    },
    [SET_WEBSITE_INFO]: (state, websiteInfo) => {
      state.websiteInfo = websiteInfo
    }
  }

}

export default BlogIndex
